/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package clusterchart.lib;

import java.awt.Graphics2D;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

/**
 *
 * @author Libra
 */
public class DrawArrow {

    Graphics2D g2d;
    double arrowLength;
    double arrowAngle;

    public void setGraphics(Graphics2D g) {
        g2d = g;
        arrowLength = 30;
        arrowAngle = 20;
    }

    public void setArrowLength(int l) {
        arrowLength = l;
    }

    public void setArrowAngle(int a) {
        arrowAngle = a;
    }

    public void drawArrow(Point2D fromPt, Point2D toPt) {
        g2d.draw(new Line2D.Double(fromPt, toPt));
        drawArrowHead(fromPt, toPt);
    }

    private void drawArrowHead(Point2D fromPt, Point2D toPt) {



        double arrowRad = arrowAngle / 180 * 3.14;
        Point2D.Double cwP, ccwP;
        double lineDeg, cwDeg, ccwDeg;

        lineDeg = Math.atan2(fromPt.getY() - toPt.getY(), fromPt.getX() - toPt.getX());
        cwDeg = lineDeg + arrowRad;
        ccwDeg = lineDeg - arrowRad;

        cwP = new Point2D.Double(toPt.getX() + arrowLength * Math.cos(cwDeg),
                toPt.getY() + arrowLength * Math.sin(cwDeg));
        ccwP = new Point2D.Double(toPt.getX() + arrowLength * Math.cos(ccwDeg),
                toPt.getY() + arrowLength * Math.sin(ccwDeg));

        g2d.draw(new Line2D.Double(toPt, cwP));
        g2d.draw(new Line2D.Double(toPt, ccwP));

    }
}
